package question2;


import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Worker implements Callable<ConcurrentHashMap<Integer, Integer>> {

    private int workerId;

    private ConcurrentHashMap<Integer, Integer> result;

    private ConcurrentLinkedQueue<Task> taskQueue;

    public Worker(int workerId, ConcurrentLinkedQueue<Task> taskQueue) {
        this.workerId = workerId;
        this.taskQueue = taskQueue;
        this.result = new ConcurrentHashMap<>();
    }

    @Override
    public ConcurrentHashMap<Integer, Integer> call() {
        while (!taskQueue.isEmpty()) {
            Task task = taskQueue.poll();
            int i = task.doTask(this);
            String log = String.format("work(workId: %s) Task[taskId:%s, param:%s] res: %s", workerId, task.getTaskId(), task.getParam(), i);
            System.out.println(log);
            this.result.put(task.getTaskId(), i);
        }
        return this.result;
    }

    public int getWorkerId() {
        return workerId;
    }

    public void setWorkerId(int workerId) {
        this.workerId = workerId;
    }

    public ConcurrentLinkedQueue<Task> getTaskQueue() {
        return taskQueue;
    }

    public void setTaskQueue(ConcurrentLinkedQueue<Task> taskQueue) {
        this.taskQueue = taskQueue;
    }

    public ConcurrentHashMap<Integer, Integer> getResult() {
        return result;
    }

    public void setResult(ConcurrentHashMap<Integer, Integer> result) {
        this.result = result;
    }
}
